package com.ag.zhh.action;

import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.zhh.service.CurrentEventService;
import com.ag.zhh.service.EventPlanService;

public class EndProcessingEventAction extends BaseAction {
	/**
	 * 结束采取紧急措施的应急流程
	 * @return
	 */
	public String processEventEnd() {
		String eventId = endProcessEvent();
		System.out.println("结束应急处置流程：采取紧急措施，事件ID——"+eventId);
		
		// 事件ID应为null，测试removeSession属性是否成功
		System.out.println("结束应急处置流程：采取紧急措施，事件ID——"+this.getSessionAttribute(EventUtil.PROCESSING_EVENT_ID));
		
		this.setResult("success");
		return "processEventEnd";
	}
	
	/**
	 * 结束启动预案的应急流程
	 * @return
	 */
	public String processEventEndByPlan() {
		String eventId = endProcessEvent();
		System.out.println("结束应急处置流程：启动预案，事件ID——"+eventId);
		
		// 事件ID应为null，测试removeSession属性是否成功
		System.out.println("结束应急处置流程：启动预案，事件ID——"+this.getSessionAttribute(EventUtil.PROCESSING_EVENT_ID));
		
		this.setResult("success");
		return "processEventEndByPlan";
	}
	

	/**
	 * 退出应急流程
	 * @return
	 */
	public String exitProcessing() {
		System.out.println("退出应急处置流程");
		clearSession();
		
		// 事件ID应为null，测试removeSession属性是否成功
		System.out.println("退出应急处置流程：事件ID——"+this.getSessionAttribute(EventUtil.PROCESSING_EVENT_ID));
	
		this.setResult("success");
		return "exitProcessing";
	}
	
	private String endProcessEvent() {
		String eventId = (String) getSession().getAttribute(EventUtil.PROCESSING_EVENT_ID);
		clearSession();
		deleteCurrentEventByEId(eventId);
		return eventId;
	}
	
	private void clearSession() {
		if(getSession().getAttribute(EventUtil.PROCESSING_EVENT_ID) != null)
			getSession().removeAttribute(EventUtil.PROCESSING_EVENT_ID);
		if(getSession().getAttribute(EventUtil.PROCESSING_PLAN_ID) != null)
			getSession().removeAttribute(EventUtil.PROCESSING_PLAN_ID);
	}
	
	/**
	 * 删除tb_current_event表的相应记录，并更改事件状态
	 * @param eventId
	 */
	private void deleteCurrentEventByEId(String eventId) {
		currentEventService.endProcessByCurrentEvent(eventId);
	}
	
	private CurrentEventService currentEventService;
	
	private String eventId;
	
	private String planId;
	
	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public String getPlanId() {
		return planId;
	}

	public void setPlanId(String planId) {
		this.planId = planId;
	}

	public CurrentEventService getCurrentEventService() {
		return currentEventService;
	}

	public void setCurrentEventService(CurrentEventService currentEventService) {
		this.currentEventService = currentEventService;
	}
	
}
